package testview.zhen.com.myapplication.asm_demo

import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

/**
 * @author :create by ldr
 * time:2022/10/27 16:05
 * @descript:添加代码的visitor
 */
class AddCodeClassVisitor(classVisitor: ClassVisitor) : ClassVisitor(Opcodes.ASM9, classVisitor) {
    override fun visitMethod(
        access: Int,
        name: String?,
        descriptor: String?,
        signature: String?,
        exceptions: Array<out String>?
    ): MethodVisitor {

        val methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions)
        return if (name.equals("toString")){
            ChangeAdapter(Opcodes.ASM9,methodVisitor,access,name,descriptor)
        }else{
            methodVisitor
        }
    }
    override fun visitEnd() {
        super.visitEnd()
        cv.visitField(
            Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "timer",
            "J", null, null
        )?.visitEnd()
    }
}